﻿namespace MetaVideoEditorEvolved.ViewModel
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using GalaSoft.MvvmLight;
	using GalaSoft.MvvmLight.Command;
	using System.Collections.ObjectModel;
	using MetaVideoEditorEvolved.Helper;

	public class EditMainDataWindowVM : ViewModelBase
	{
		private string title;

		private string overview;

		private bool isDirty;

		private int? runningTime;

		private UI.Entity.VideoMediaElement media;

		private string mediaType;

		private ObservableCollection<string> availableMediaTypes;

		private RelayCommand acceptChangeCommand;

		private RelayCommand cancelCommand;

		public RelayCommand CancelCommand
		{
			get
			{
				return cancelCommand;
			}

			set
			{
				cancelCommand = value;
				RaisePropertyChanged("CancelCommand");
			}
		}

		public RelayCommand AcceptChangeCommand
		{
			get
			{
				return acceptChangeCommand;
			}

			set
			{
				acceptChangeCommand = value;
				RaisePropertyChanged("AcceptChangeCommand");
			}
		}

		public ObservableCollection<string> AvailableMediaTypes
		{
			get
			{
				return availableMediaTypes;
			}

			set
			{
				if (this.availableMediaTypes != value)
				{
					availableMediaTypes = value;
					RaisePropertyChanged("AvailableMediaTypes");

					this.IsDirty = true;
				}
			}
		}

		public string MediaType
		{
			get
			{
				return mediaType;
			}

			set
			{
				if (this.mediaType != value)
				{
					mediaType = value;
					RaisePropertyChanged("MediaType");

					this.IsDirty = true;
				}
			}
		}

		public string Title
		{
			get
			{
				return title;
			}

			set
			{
				if (this.title != value)
				{
					title = value;
					RaisePropertyChanged("Title");

					this.IsDirty = true;
				}
			}
		}

		public int? RunningTime
		{
			get
			{
				return runningTime;
			}

			set
			{
				if (this.runningTime != value)
				{
					runningTime = value;
					RaisePropertyChanged("RunningTime");

					this.IsDirty = true;
				}
			}
		}


		public string Overview
		{
			get
			{
				return overview;
			}

			set
			{
				if (this.overview != value)
				{
					overview = value;
					RaisePropertyChanged("Overview");

					this.IsDirty = true;
				}
			}
		}

		public bool IsDirty
		{
			get
			{
				return isDirty;
			}

			set
			{
				isDirty = value;
				RaisePropertyChanged("IsDirty");
			}
		}


		public EditMainDataWindowVM(UI.Entity.VideoMediaElement media)
			: base()
		{
			this.media = media;
			
			this.title = this.media.Title;
			this.overview = this.media.Overview;
			this.runningTime = this.media.RunningTime;
			this.mediaType = this.media.MediaType;

			var helper = this.CreateKernelHelper();
			var mediaTypes = helper.ExtractAvailableMediaTypes();
			mediaTypes.Add(helper.GetString("MediaTypeDvd"));
			mediaTypes.Add(helper.GetString("MediaTypeBlueRay"));
			mediaTypes.Add(helper.GetString("MediaTypeCd"));

			this.availableMediaTypes = new ObservableCollection<string>();
			foreach (var item in mediaTypes.Distinct())
			{
				this.availableMediaTypes.Add(item);
			}

			this.acceptChangeCommand = new RelayCommand(() =>
			                                            {
			                                            	this.media.HasChanged = true;
			                                            	this.media.Title = this.Title;
			                                            	this.media.Name = this.Title;
			                                            	this.media.Overview = this.Overview;
			                                            	this.media.RunningTime = this.RunningTime;
			                                            	this.media.MediaType = this.MediaType;
			                                            	
			                                            	var mveItem = UI.KernelSessionManager.SessionInstance.RealKernel.ItemCollection.FindById(this.media.Id.ToString());
			                                            	mveItem.HasChanged = true;
			                                            	mveItem.Title = this.media.Title;
			                                            	mveItem.Overview = this.media.Overview;
			                                            	mveItem.RunningTime = this.media.RunningTime;
			                                            	mveItem.Mediatype = this.media.MediaType;
			                                            				                                            			                                            
			                                            	Messages.CloseMessage msg = new Messages.CloseMessage();
			                                            	GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(msg);
			                                            },
			                                            () =>
			                                            {
			                                            	return this.IsDirty;
			                                            });

			this.cancelCommand = new RelayCommand(() =>
			                                      {
			                                      	Messages.CloseMessage msg = new Messages.CloseMessage();
			                                      	GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(msg);
			                                      });
		}

		protected virtual IKernelHelper CreateKernelHelper()
		{
			return new KernelHelper();
		}
	}
}
